import App from './App'

// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'

try {
  function isPromise(obj) {
    return (
      !!obj &&
      (typeof obj === "object" || typeof obj === "function") &&
      typeof obj.then === "function"
    );
  }

  // 统一 vue2 API Promise 化返回格式与 vue3 保持一致
  /* uni.addInterceptor({
  	returnValue(res) {
  		if (!isPromise(res)) {
  			return res;
  		}
  		return new Promise((resolve, reject) => {
  			res.then((res) => {
  				//console.log(res[0])
  				if (res[0]) {
  					try {
  						reject(res[0]);
  					} catch (e) {
  						//TODO handle the exception
  					}

  				} else {
  					resolve(res[1]);
  				}
  			});
  		});
  	},
  }); */
} catch (error) {}

const app = new Vue({
  ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import {
  createSSRApp
} from 'vue'

export function createApp() {
  const app = createSSRApp(App)
  return {
    app
  }
}
// #endif


//封装弹框的方法
uni.$showMsg = function(title = '数据请求失败！！！', duration = 1500) {
  uni.showToast({
    title,
    duration,
    icon: 'none'
  })
}


//引入vuex
import store from '@/store/index.js'
//console.log(store)
Vue.prototype.$store = store
uni.$store = store

//引入enums
import {
  chatType
} from '@/components/enums/enums.js'
Vue.prototype.$chatType = chatType
uni.$chatType = chatType


Vue.prototype.$isLoginByVisitor = async function() {
  //console.log(123)
  //console.log(store)
  if (!uni.getStorageSync('isVisitorLogin')) {
    setTimeout(() => {
      uni.showToast({
        title: '还未登录请先登录',
        duration: 1500,
        icon: 'error'
      })
      uni.navigateTo({
        url: '/pages/login/login/login'
      })
    }, 10)
  }
}
const audio = require('@/static/audio/y1478.wav');
let audioPlaying = false
//Vue.prototype.$chatType = chatType
uni.$isPromptPlay = function() {
  if (audioPlaying) return
  const innerAudioContext = uni.createInnerAudioContext();
  innerAudioContext.autoplay = true;
  innerAudioContext.src = audio;
  innerAudioContext.onPlay(() => {
    console.log('开始播放');
    audioPlaying = true
  });
  innerAudioContext.onEnded(() => {
    console.log('停止播放');
    innerAudioContext.destroy()
    audioPlaying = false
  });
  innerAudioContext.play();
}

//获取当前使用设备类型
// Vue.prototype.$deviceType = ''
// uni.$deviceType = ''


//端口前缀（）
//var port = '192.168.88.1'
//var port = '47.106.223.49'
var port = '47.107.35.139'
//var port = 'www.kexingkj.cn'
uni.$basePort = port
uni.$nettyPort = '47.107.35.139'
import websocket from '@/utils/msg/connectSocketInit.js'
//连接上websocket
uni.$connectSocketInit = websocket


uni.$isShowModal = function(content = '确定删除？', title = '提示') {
  return new Promise((resolve, rejected) => {
    uni.showModal({
      title: title,
      content: content,
      cancelText: "取消", // 取消按钮的文字  
      confirmText: "确认", // 确认按钮的文字  
      showCancel: true, // 是否显示取消按钮，默认为 true
      /* confirmColor: '#f55850',
      cancelColor: '#39B54A', */
      success: (res) => {
        if (res.confirm) {
          resolve(true)
          //console.log('comfirm') //点击确定之后执行的代码
        } else {
          resolve(false)
          console.log('cancel') //点击取消之后执行的代码
        }
      },
      fail: function(err) {
        //console.log(err.errMsg);
        rejected(err)
      }
    })

  });
}